$(function () {
    //状态按钮切换
    $('table').on('click', '.status-btn', function () {
        if ($(this).is('.btn-use')) {
            $(this).removeClass('btn-use').addClass('btn-nouse').html('<i class="fa fa-ban"></i> 禁用');
        } else {
            $(this).removeClass('btn-nouse').addClass('btn-use').html('<i class="fa fa-check-square-o"></i> 启用');
        }
    })


    //编辑
    $('#brandSetting tbody').on('click', '.edit', function () {
    
        var tds = $(this).parent().siblings();
        //品牌
        var brand = tds.eq(3).text().trim();

        tds.eq(0).html('');
        tds.eq(1).html(tds.eq(1).text());
        tds.eq(2).html(tds.eq(2).text());
        tds.eq(3).html('<input type="text" name="brandName" class="input-sm form-control brandName" value="' + tds.eq(3).text().trim() + '">');

        //按钮切换
        $(this).removeClass('edit btn-info').addClass('save btn-use');
        $(this).html('<i class="fa fa-copy"></i> 保存');
        $(this).next().removeClass('delete btn-danger').addClass('cancel btn-warning');
        $(this).next().html('<i class="fa fa-times"></i> 取消');

        //保存
        $('#brandSetting tbody').on('click', '.save', function () {
        	var tds = $(this).parent().siblings();
        	var brandId = tds.eq(1).text();
        	var brandName = $('.brandName').val();
        	$.ajax({
                url: ctx + '/bizBrand/updateRecord',
                type: 'POST',
                data: {
                	brandId:brandId,
                	brandName:brandName
                },
                dataType: 'json',
                success: function (data) {
                	if(data.status == 'success'){
                		DataTable.init();
                	}else{
                		toastr.error(data.msg);
                	}
                }
        	});

            //按钮切换
            $(this).removeClass('save btn-use').addClass('edit btn-info');
            $(this).html('<i class="fa fa-edit"></i> 编辑');
            $(this).next().removeClass('cancel btn-warning').addClass('delete btn-danger');
            $(this).next().html('<i class="fa fa-trash-o"></i> 删除');
        })

        //取消
        $('#brandSetting tbody').on('click', '.cancel', function () {
        	
            tds.eq(0).html('<label><input type="checkbox"><span class="text"></span></label>');
            tds.eq(3).html(brand);

            //按钮切换
            $(this).removeClass('cancel btn-warning').addClass('delete btn-danger');
            $(this).html('<i class="fa fa-trash-o"></i> 删除');
            $(this).prev().removeClass('save btn-use').addClass('edit btn-info');
            $(this).prev().html('<i class="fa fa-edit"></i> 编辑');
        })
    })

    //删除
    $('#brandSetting tbody').on('click', '.delete', function () {
    	var tds = $(this).parent().siblings();
    	var brandId = tds.eq(1).text();
    	$.ajax({
            url: ctx + '/bizBrand/updateRecord',
            type: 'POST',
            data: {
            	brandId:brandId,
            	deleteMark:0
            },
            dataType: 'json',
            success: function (data) {
            	if(data.status == 'success'){
            		DataTable.init();
            	}else{
            		toastr.error(data.msg);
            	}
            }
    	});
    })
})

//新增品牌
$('#brandAdd').bootstrapValidator({
	message: 'This value is not valid',
	 fields: {
		 brand: {
            validators: {
                notEmpty: {
                    message: '金额不能为空'
                }
            }
        },
	 },
	 submitHandler: function (validator, form, submitButton) {
		 var options = form.serialize();
		 $.ajax({
			 type: "POST",
             url:  ctx + '/bizBrand/addNewRecord',
             data: options,
             dataType: 'json',
             success: function (data) {
            	 DataTable.init();
            	 $('.brandAdd').modal('hide');
             },
             error: function (msg) {
             	toastr.error("系统错误");
             }
		 });
	 }
});

//品牌初始化数据
var DataTable = function(){
	return{
		init: function(){
			var Table = $('#brandSetting').dataTable({
				"bPaginate": true,  //是否显示分页
            	"iDisplayLength": 15,
            	"bLengthChange": false,//每页显示的记录数
            	"bFilter": false, //搜索栏
            	"bSort": true, //是否支持排序功能
            	"bInfo": true, //显示表格信息
            	"bAutoWidth": false,  //自适应宽度
            	"bStateSave": true, //保存状态到cookie *************** 很重要 ， 当搜索的时候页面一刷新会导致搜索的消失。使用这个属性就可避免了
            	//"sPaginationType": "", //分页，一共两种样式，full_numbers和two_button(默认)
            	"sAjaxSource" : ctx+'/bizBrand/load',
        		"fnServerData": retrieveData,//用于替换默认发到服务端的请求操作  
            	"bServerSide": true,
            	"bDestroy": true,
                "bRetrieve": false,
                "oLanguage" : {
        			"sLengthMenu" : "每页显示 _MENU_ 条记录",
        			"sZeroRecords" : "抱歉， 没有找到",
        			"sInfo" : "从 _START_ 到 _END_ / 共 _TOTAL_ 条数据",
        			"sInfoEmpty" : "找不到相关数据",
        			"sInfoFiltered" : "数据表中共为 _MAX_ 条记录)",
        			"sProcessing": "正在加载中...",
        			"sSearch": "搜索",
        			"oPaginate" : {
        				"sFirst" : "首页",
        				"sPrevious" : "前一页",
        				"sNext" : "后一页",
        				"sLast" : "尾页"
        			},
        		},
        		"aoColumns" : [
       			            {
       			                "mData": "brandId", 'sClass': "text-center", "bSortable": false, "mRender": function (data, type, full) {
       			                return '<label  class="labletab" style="padding-top: 0px"> <input name="ajaxcheckbox" type="checkbox" class="checkchild"> <span class="text" ></span> </label>';
       			            }
       			            },
       			            {"mData": "brandId", 'sClass': "hiddenCol"},
       			            {"mData": "brandCode", 'sClass': "text-center"},
       			            {"mData": "brandName", 'sClass': "text-center"},
       			            {"mData": "enable", 'sClass': "text-center","bSortable": false,"mRender":function(data, type, full ){
       			            	if(data==0){
       			          		  return '<span id="span'+full["brandId"]+'" onclick="chooseStudent(\''+full["brandId"]+'\')" class="btn btn-xs btn-nouse"><i class="fa fa-ban"></i> 禁用</span>';
       			          	  }else{
       			          		  return '<span id="span'+full["brandId"]+'" onclick="chooseStudent(\''+full["brandId"]+'\')" class="btn btn-xs btn-use"><i class="fa fa-check-circle-o"></i> 启用</span>';
       			          	  }
       			            }},
       			            {
       			                "mData": "brandId",
       			                'sClass': "text-center",
       			                "bSortable": false,
       			                "mRender": function (data, type, full ) {
       			                    var u = '<a href="#" class="btn btn-info btn-xs edit" '+(full.enable == 0 ? "disabled" : "")+'><i class="fa fa-edit"></i> 编辑</a>';
       			                    var d = '<a href="#" class="btn btn-danger btn-xs delete"><i class="fa fa-trash-o"></i> 删除</a>';
       			                    return u + d;
       			                }
       			            }
       			        ],
       			     "aoColumnDefs": [{
 		   	            sDefaultContent: '',
 		   	            aTargets: ['_all']
 		   	        }],
			})
		}
	}
}();
DataTable.init();
/**
 * 回调函数
 * @param sSource
 * @param aoData
 * @param fnCallback
 * @returns
 */
function retrieveData( sSource, aoData, fnCallback, oSettings ) {
	aoData.push( { "name": "pageNum", "value": (Math.ceil( oSettings._iDisplayStart / oSettings._iDisplayLength )+1) });
    aoData.push( { "name": "pageSize", "value": oSettings._iDisplayLength });  
    
    var searchVal = $('#searchVal').val();
    
    if(searchVal && searchVal.length != 0){
    	aoData.push({ "name": "searchVal", "value": searchVal});
    }
    
    $.ajax( {  
        "url": sSource,  
        "data": aoData,  
        "cache": false,  
        "dataType": 'json', 
        "type": "POST", 
        "success" :function(response) {
        	fnCallback(response.returnObject);
        }  
    } );  
};

//修改状态
function chooseStudent(val,flag){
	var attr = $("#span"+val).attr("class");
	if(attr == "btn btn-xs btn-nouse"){
		flag = 1;
	}else{
		flag = 0;
	}
	$.ajax({
        url: ctx + '/bizBrand/updateRecord',
        type: 'POST',
        data: {
        	brandId:val,
        	enable:flag
        },
        dataType: 'json',
        success: function (data) {
        	/*if(data.status == 'success' && flag == 1){
        		$("#span"+val).removeClass("btn-nouse").addClass("btn-use");
        		$("#span"+val).html('<i class="fa fa-check-circle-o"></i> 启用');
        	}
        	if(data.status == 'success' && flag == 0){
        		$("#span"+val).removeClass("btn-use").addClass("btn-nouse");
        		$("#span"+val).html('<i class="fa fa-ban"></i> 禁用');
        	}*/
        	
        	DataTable.init();
        }
	});
}
//回车搜索
function search(){
	if(event.keyCode==13){
		DataTable.init();
	}
}